package com.ljt.base.wheel;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.aigestudio.wheelpicker.widgets.WheelDatePicker;
import com.ljt.base.R;
import com.ljt.base.dialog.BaseDialog;
import com.ljt.base.dialog.ViewHolder;
import com.ljt.base.listener.OnDialogSelectListener;
import com.ljt.base.utils.LogUtil;

import java.util.Calendar;

public class DateDialog extends BaseDialog implements View.OnClickListener {

    private Context mContext;
    private OnDialogSelectListener listener;
    private WheelDatePicker wheelDatePicker;
    private TextView tvModuleCancel, tvModuleSubmit;

    public DateDialog(@NonNull Context context) {
        super(context);
        this.setContentView(R.layout.dialog_date_select);
        this.mContext = context;
    }

    public void setOnDialogSelectListener(OnDialogSelectListener listener) {
        this.listener = listener;
    }

    /**
     * 初始化界面
     *
     * @return
     */
    @Override
    public int intLayoutId() {
        return R.layout.dialog_date_select;
    }

    /**
     * 逻辑处理方法
     *
     * @param holder
     * @param dialog
     */
    @Override
    public void convertView(ViewHolder holder, BaseDialog dialog) {
        wheelDatePicker = holder.getView(R.id.wdModule);
        Calendar CD = Calendar.getInstance();
        int YY = CD.get(Calendar.YEAR);
        int MM = CD.get(Calendar.MONTH) + 1;
        int DD = CD.get(Calendar.DATE);
        wheelDatePicker.setYear(YY);
        wheelDatePicker.setMonth(MM);
        wheelDatePicker.setSelectedDay(DD);
        LogUtil.INSTANCE.e(YY + "年" + MM + "月" + DD + "日");
        TextView tvModuleYear = wheelDatePicker.getTextViewYear();
        setTextRes(tvModuleYear, "Year");
        TextView tvModuleMonth = wheelDatePicker.getTextViewMonth();
        setTextRes(tvModuleMonth, "Month");
        TextView tvModuleDay = wheelDatePicker.getTextViewDay();
        setTextRes(tvModuleDay, "Day");
        wheelDatePicker.setItemTextColor(mContext.getResources().getColor(R.color.black));
        wheelDatePicker.setItemTextSize(36);
        //wheelDatePicker.setCurtain(true);
        wheelDatePicker.setAtmospheric(true);
        //设置弯曲
        wheelDatePicker.setCurved(true);

        tvModuleCancel = holder.getView(R.id.tvModuleCancel);
        tvModuleSubmit = holder.getView(R.id.tvModuleSubmit);
        tvModuleCancel.setOnClickListener(this);
        tvModuleSubmit.setOnClickListener(this);
        setDimAmount(0.80f);
    }

    public void setTextRes(TextView tvModule, String str) {
        tvModule.setTextColor(mContext.getResources().getColor(R.color.black));
        tvModule.setText(str);
    }

    private String month, day;
    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvModuleCancel) {
            dismiss();
        } else if (id == R.id.tvModuleSubmit) {
            LogUtil.INSTANCE.e("点击确定");
            if (listener != null) {
                month = wheelDatePicker.getCurrentMonth() > 9 ? String.valueOf(wheelDatePicker.getCurrentMonth()) : "0" + wheelDatePicker.getCurrentMonth();
                day = wheelDatePicker.getCurrentDay() > 9 ? String.valueOf(wheelDatePicker.getCurrentDay()) : "0" + wheelDatePicker.getCurrentDay();
                listener.setOnDialogSelectListener(wheelDatePicker.getCurrentYear() + "/" +
                        month + "/" +
                        day);
            } else {
                dismiss();
            }
        }
    }
}

